package com.bibrApp.difu.base;


import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;


import com.bibrApp.difu.R;
import com.bibrApp.difu.actvity.login_mvp.LoginActivity;
import com.bibrApp.difu.utils.AppManager;
import com.bibrApp.difu.utils.DialogUtils;
import com.bibrApp.difu.utils.IntentUtil;
import com.bibrApp.difu.utils.NetWorkUtil;
import com.bibrApp.difu.utils.ToastUtils;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public abstract class BaseObserver<T> implements Observer<NewBaseReponse<T>> {
    private Context mContext;
    private boolean isShowDialog;
    private Disposable mDisposable;

    public BaseObserver(Context mContext, boolean isShowDialog) {
        this.mContext = mContext;
        this.isShowDialog = isShowDialog;
    }

    public BaseObserver(Context mContext) {
        this.mContext = mContext;
        this.isShowDialog = true;
    }

    @Override
    public void onSubscribe(Disposable d) {
        try {
            if(NetWorkUtil.isNetWorkConnected(mContext)){
                if(isShowDialog){
                    DialogUtils.showDialog(mContext);
                }
            }else{
                ToastUtils.ShowToast(mContext,mContext.getResources().getString(R.string.net_work_not_connected));
                if(d.isDisposed()){
                    d.dispose();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onNext(NewBaseReponse<T> value) {
        try{
            DialogUtils.dismissDialog();
            int code = value.getCode();
            if(isTokenExpired(code)){
                if(code == 602){//需要刷新token
                    onSuccessOfCode(code , value.getMessage());
                }else{
                    gotoLoginActivity(value.getMessage());
                }
            }else if(code == 200){
                onSuccess(value.getData());
            }else if(code > 500 && code < 600){
                if(code != 501){
                    onSuccessOfCode(code,value.getMessage(),String.valueOf(value.getData()));
                }
//                gotoLoginActivity(value.getMessage());
            }else if(code == 500){
                onFialure(code,value.getMessage());
                ToastUtils.ShowToast(mContext,value.getMessage());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void gotoLoginActivity(String msg){
            if (!TextUtils.isEmpty(msg)) {
                ToastUtils.ShowToast(mContext, msg);
            } else {
                ToastUtils.ShowToast(mContext, "token过期,请重新登陆");
            }
            IntentUtil.toActivity(LoginActivity.class, mContext);
    }
    private boolean isTokenExpired(int code){
        if(code >= 600 && code < 700){
            return true;
        }
        return false;
    }

    @Override
    public void onError(Throwable e) {
        DialogUtils.dismissDialog();
        Activity activity = AppManager.getTopActivity();
        if(activity != null){
            ToastUtils.ShowToast(AppManager.getTopActivity(), "服务器异常");
        }
        e.printStackTrace();
    }

    @Override
    public void onComplete() {
        if(mDisposable != null && mDisposable.isDisposed()){
            mDisposable.isDisposed();
            mDisposable = null;
        }
        DialogUtils.dismissDialog();
    }

    //请求成功的回调函数
    protected abstract void onSuccess(T value);

    //code!=200,即请求失败的回调,需要时可重写
    protected void onFialure(int code , String msg){}

    protected void onSuccessOfCode(int code , String msg){}

    protected void onSuccessOfCode(int code , String msg ,String account){}
}
